package cn.tedu.v6;

import java.io.*;
import java.net.ConnectException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

/**
 *Version 6 退出和在线用户列表
 */

public class Client {
    private Socket socket;
    private static final String HOST="172.20.10.10";
    private static final Integer PORT=1111;
    private BufferedReader br;
    private PrintWriter pw;
    private String ip;

    public Client() {
        try {
            socket=new Socket(HOST,PORT);
            br=new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF8"));
            pw=new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8")), true);
        } catch (NullPointerException e){
            System.out.println("IP传入错误导致链接失败");
        } catch (UnknownHostException e){
            System.out.println("IP传入出错");
        } catch (ConnectException e){
            System.out.println("客户端链接失败：建议检查IP或者端口号");
        } catch (IOException e) {
            System.out.println("客户端链接服务器失败，提示：检查IP/端口");
        }
    }

    public void start(){
        System.out.println("欢迎使用RH牌QQ聊天室");
        Scanner input=new Scanner(System.in);
        try {
            //1.读入服务器端
            String serverMessage=br.readLine();
            //1.1显示”请输入用户名：“
            System.out.println(serverMessage);
            //1.2 写出用户名
            String username=input.nextLine();
            //1.3用户输入的用户名--写出给服务器端
            pw.println(username);

            //2.1读入服务器端发来 ”请输入密码“
            serverMessage=br.readLine();
            System.out.println(serverMessage);
            //2.2用户输出--密码--写出给服务器端
            String password=input.nextLine();
            pw.println(password);

            //3.登录之后【成功、失败】--才可以聊天
            serverMessage=br.readLine();//读入【成功、失败】
            //字符串方法：startWith() 返回值 boolean 前缀名称一致true
            if (serverMessage.startsWith("登录成功")){
                //封一个客户端：读写流--chat聊天
                chat();
            }
        } catch (IOException e){
            System.out.println("客户端断开连接了");
        }finally {
            if (br != null)
                try {
                    br.close();
                } catch (IOException e) {
                    System.out.println("客户端关闭失败");
                }
            if (pw != null)
                pw.close();
        }if(socket!=null&&!socket.isClosed())
            try {
                socket.close();
            }catch (IOException e) {
                System.out.println("客户端关闭失败");
            }
        if (socket!= null &&!socket.isClosed())
            try {
                socket.close();
            } catch (IOException e) {
                System.out.println("通讯关闭失败");
            }
    }
    private void chat() {
        new Thread(() -> {
            try {

                String message;
                while ((message = br.readLine()) != null) {
                    System.out.println("客户端请输入");
                    System.out.println("服务器说：" + message);
                    if (message.equals(socket)){
                        pw.println("请用户输入(输入exit退出)："+pw);
                    }
                }
            } catch (IOException e) {
                System.out.println("服务器端读入失败");
            }
        }).start();
        Scanner input=new Scanner(System.in);
        while (true){
            System.out.println("客户端请输入");
            String line=input.nextLine();
            pw.println("客户端说:"+line);
            if(line.equalsIgnoreCase("exit")){
                //如果输入为exit，关闭链接
                pw.println("exit");//打印exit
                System.out.println(ip+"退出聊天室");
                System.exit(0);
        }
    }
    }
    public static void main(String[] args) throws IOException {
            new Client().start();
        }
    }